var MENU_BAR_HASH = {};
var needOpen = [];
(function () {
  companySetMenuData(CompanyMenuList);
})(jQuery);
/**菜单字体颜色 */
function setColor(item) {
  var style = '';
  if (item.color) {
    style = 'color:' + item.color
  }
  return style;
}
/**初始化菜单 */
function companySetMenuData(list) {
  var menu = {};
  var htm = "";
  var first = "";
  for (var j = 0; j < list.length; j++) {
    var _class = "";
    var item = list[j];
    if (j == 0) {
      _class = " active";
      menu = item;
    }
    htm += '<div data-noclose="' + item.noClose + '" data-id="' + item.id + '" id="' + item.id + '" class="menu_item' + _class + '" data-index="' + j + '">' +
      '<span class="iconfont ' + item.icon + '"></span>' +
      '<span class="text">' + item.name + '</span>' +
      '</div>'
  }
  // company_init_menu(menu.children);
  $("#asidMenu").html(htm);
  if (menu.children && menu.children.length) {
    showChildMenu();
    setMenuTitle(menu.name);
    company_init_menu(menu.children);
  } else {
    setMenuTitle('');
    CompanyAddTab(menu, menu.id);
  }
  $("#asidMenu .menu_item")
    .off("click")
    .on("click", function () {
      $(this).addClass('active').siblings().removeClass('active');
      var index = $(this).data('index');
      if (index == 0) {
        setMenuTitle('');
      }
      CompanyAddTab(list[index], list[index].id);
      var children = list[index].children;
      var otherList = list[index].otherList || [];
      if (children && children.length) {
        company_init_menu(children, list[index].id, otherList);
        setMenuTitle(list[index].name);
        showChildMenu();
      } else {
        hideChildMenu();
      }
    });
}
function emptyHtml() {
  $("#menuBox").html("");
}
function init_menu(menus, index) {
  if (!index) index = 0
  if (!Array.isArray(menus)) menus = new Array();

  var chooseItem = {};

  var str = '<ul class="menuItem">';
  for (var i = 0; i < menus.length; i++) {
    var item = menus[i];
    var li_c_class = "";
    if (item.children && item.children.length) {
      li_c_class = "hasChild";
      if ((i == 0 && index == 0) || item.flagExpand) {
        li_c_class += " showMenu";
      }
    } else if (i == 0 && index == 0) {
      li_c_class = "active";
      chooseItem = item;
    }
    str +=
      '<li class="' +
      li_c_class +
      '"><a class="' +
      (item.icon ? "hasIcon iconfont " + item.icon : "noIcon") +
      '" data-id="' + item.id + '" id="menu_' +
      item.id +
      '" href="javascript:void(0);" data-src="' +
      item.path +
      '" data-name="' +
      item.title +
      '"><span class="menuText" style="' + setColor(item) + '">' +
      item.title;
    if (item.notice) {
      str += '<span class="number">' + item.notice + '</span>'
    }
    str += "</span>";

    if (item.children && item.children.length) {
      str += '<i class="iconfont icon-arrow-down"></i></a>';
      str += '<ul class="childMenu">';
      for (var j = 0; j < item.children.length; j++) {
        var item_c = item.children[j];
        var li_class = "";
        if (item_c.children && item_c.children.length) {
          li_class = "hasChild";
          if ((j == 0 && index == 0) || item_c.flagExpand) {
            li_class += " showMenu";
          }
        } else if (i == 0 && j == 0 && index == 0) {
          li_class = "active";
          chooseItem = item_c;
        }
        if (item_c.flagOpen) {
          needOpen.push(item_c)
        }
        str +=
          '<li class="' +
          li_class +
          '" title="' +
          item_c.title +
          '"><a data-id="' + item_c.id + '" id="menu_' +
          item_c.id +
          '" href="javascript:void(0);" class="' +
          (item_c.icon ? "hasIcon iconfont " + item_c.icon : "noIcon") +
          '" data-src="' +
          item_c.path +
          '" data-name="' +
          item_c.title +
          '"><span class="menuText" style="' + setColor(item_c) + '">' +
          item_c.title;
        if (item_c.notice) {
          str += '<span class="number">' + (item_c.notice > 99 ? '99+' : item_c.notice) + '</span>'
        }
        str += "</span>";
        if (item.flagFast) {
          var d_class = 'icon-tudingguding'
          if (item.flagFastExist) {
            d_class = 'icon-quxiaogudingjiedian'
          }
          str += '<i class="iconfont ' + d_class + ' fastAdd"></i>'
        }
        if (item_c.children && item_c.children.length) {
          str += '<i class="iconfont icon-arrow-down"></i></a>';
          str += '<ul class="childMenu">';
          for (var k = 0; k < item_c.children.length; k++) {
            var item_child = item_c.children[k];
            if (i == 0 && j == 0 && k == 0) {
              chooseItem = item_child;
            }
            if (item_child.flagOpen) {
              needOpen.push(item_child)
            }
            str +=
              '<li title="' +
              item_child.title +
              '"><a class="noIcon" id="' + item_child.id + '" data-id="' +
              item_child.id +
              '" href="javascript:void(0);" data-src="' +
              item_child.path +
              '" data-name="' +
              item_child.title +
              '">' +
              '<text style="' + setColor(item) + '">' +
              item_child.title +
              '</text></a></li>';
          }
          str += "</ul>";
        } else {
          str += "</a>";
        }
        str += "</li>";
      }
      str += "</ul>";
    } else {
      str += "</a>";
    }
    // }
    str += "</li>";
  }
  str += "</ul>";
  $("#menuBox").html(str);
  if (index == 0) {
    // addTab(chooseItem);
  }
  // loadMenuAfter();
}
function company_init_menu(menus, pid, otherList) {
  var chooseItem = {};
  var str = ''
  for (var i = 0; i < menus.length; i++) {
    var item = menus[i];
    var li_c_class = "";
    if (i == 0) {
      // li_c_class = 'active'
      chooseItem = item;
    }
    if (item.children && item.children.length) {
      li_c_class = ' showChild hasChild'
    }
    str += '<li class="childItem' + li_c_class + '">';

    if (item.children && item.children.length) {
      str += '<span class="itemLink" data-pid="' + pid + '" data-index="' + i + '" id="' + item.id + '">';
      str += '<span class="iconfont icon-arrow-right"></span>'
      str += item.name
      str += '</span>';
    } else {
      str += '<a class="itemLink" href="javascript:;" data-pid="' + pid + '" data-index="' + i + '" id="' + item.id + '">';
      str += item.name
      if (item.flagFast) {
        var _class = 'icon-tudingguding'
        if (item.flagFastExist) {
          _class = 'icon-quxiaogudingjiedian'
        }
        str += '<i class="iconfont ' + _class + ' fastAdd"></i>'
      }
      str += '</a>';
    }

    if (item.children && item.children.length) {
      str += '<dl>'
      for (var j = 0; j < item.children.length; j++) {
        var childItem = item.children[j];
        str += '<dd><a class="itemLink" href="javascript:;" data-pid="' + pid + '" data-index="' + i + ',' + j + '" id="' + childItem.id + '">'
        str += childItem.name
        if (childItem.flagFast) {
          var d_class = 'icon-tudingguding'
          if (childItem.flagFastExist) {
            d_class = 'icon-quxiaogudingjiedian'
          }
          str += '<i class="iconfont ' + d_class + ' fastAdd"></i>'
        }
        str += '</a></dd>'
      }
      str += '</dl>'
    }
    str += '</li>'
    // str += "<li data-pid='" + pid + "' data-index='" + i + "' id='" + item.id + "' class='" + li_c_class + "'>" + item.name + "</li>";
  }
  if (otherList && otherList.length) {
    for (var j = 0; j < otherList.length; j++) {
      var item = otherList[j];
      var li_c_class = "";
      if (j == 0) {
        li_c_class = ' firstOther'
      }
      str += '<li class="childItem' + li_c_class + '">';
      str += '<a class="otherItem" href="javascript:;" data-pid="' + pid + '" data-index="' + j + '" id="' + item.id + '">';
      str += item.name
      str += '</a>';
      str += '</li>'
    }
  }
  $("#child_menu_list").html(str);
  $("#child_menu_list").off('click', '.itemLink').on('click', '.itemLink', function () {
    $("#child_menu_list").find('.active').removeClass('active')
    $(this).addClass('active');
    if ($(this).parent('li').hasClass('hasChild')) {
      $(this).parent('li').toggleClass('showChild')
      $(this).next('dl').slideToggle();
    }
    var index = ($(this).data('index') || $(this).data('index') === 0) ? $(this).data('index').toString().split(',') : []
    if (index.length == 1) {
      chooseItem = menus[index[0]];
    } else if (index.length > 1) {
      chooseItem = menus[index[0]].children[index[1]];
    }
    CompanyAddTab(chooseItem, pid);
  })
  $("#child_menu_list").off('click', '.otherItem').on('click', '.otherItem', function () {
    $("#child_menu_list").find('.active').removeClass('active')
    $(this).addClass('active');
    var index = $(this).data('index');
    chooseItem = otherList[index];
    CompanyAddTab(chooseItem, pid);
  })
  $("#child_menu_list").off('click', '.fastAdd').on('click', '.fastAdd', function (e) {
    e.stopPropagation();
    if ($(this).hasClass('icon-tudingguding')) {
      console.log('加入快捷菜单')
    } else {
      console.log('取消加入快捷菜单')
    }
    $(this).toggleClass('icon-tudingguding icon-quxiaogudingjiedian');

  })
  // CompayLoadMenuAfter();
}
function showChildMenu() {
  $("#child_menu_box").animate({
    width: '200px'
  }, 'fast');
  $("#child_nav_hide").fadeIn();
  $("#showAsidBtn").off('click').on('click', function () {
    $("#showAsidBtn").fadeOut();
    $("#child_nav_hide").fadeIn();
    $("#child_menu_box").animate({
      width: '200px'
    }, 'fast');
  });
  $("#child_nav_hide").off('click').on('click', function () {
    $("#child_nav_hide").fadeOut();
    $("#child_menu_box").animate({
      width: '0'
    })
    $("#showAsidBtn").fadeIn();
  });
}
function hideChildMenu() {
  setMenuTitle('');
  $("#child_menu_list").empty();
  $("#child_menu_box").animate({
    width: '0'
  })
  $("#child_nav_hide").fadeOut();

}
function setMenuTitle(name) {
  $("#menuTitle").html('<div>' + name + '</div>')
}

function CompayLoadMenuAfter() {
  $("#menuBox li a")
    .off("click")
    .on("click", function () {
      var li = $(this).parent("li");
      var href = $(this).data("src");
      var id = $(this).data("id");
      var title = $(this).data("name");
      var srcParam = $(this).attr("srcParam");
      var noClose = $(this).data("noclose");
      var paramData = {};
      if (srcParam) {
        var arr1 = srcParam.split(",");
        if (arr1) {
          $.each(arr1, function (j1, k1) {
            if (k1) {
              var arr2 = k1.split("=");
              if (arr2 && arr2.length == 2) {
                paramData[arr2[0]] = arr2[1];
              }
            }
          });
        }
      }
      var hasChild = li.hasClass("hasChild");
      if (hasChild) {
        li.toggleClass("showMenu");
        if (!li.hasClass("showMenu")) {
          li.find(".showMenu").removeClass("showMenu");
        }
      } else {
        $("#menuBox li").removeClass("active");
        setTimeout(function () {
          li.addClass("active");
        }, 100);
        if (!noClose) {
          //CompanyAddTab({ "", id, path: href, data: paramData });
          CompanyAddTab({ "title": title, "id": id, "path": href, "data": paramData });
        } else {
          $("#companyHeadTree a:first")
            .addClass("active")
            .siblings()
            .removeClass("active");
          $("#mainIframe .iframe").addClass("hidden");
          $("#homePage").removeClass("hidden");
        }
      }
      return;
    });

  //头部菜单
  $("#headNav li.more").hover(
    function () {
      $(this).find(".childNav").stop().slideDown();
    },
    function () {
      $(this).find(".childNav").stop().slideUp();
    }
  );
  $("#headNav li.dropdown").hover(
    function () {
      $(this).find(".dropdown-menu").addClass("show");
    },
    function () {
      $(this).find(".dropdown-menu").removeClass("show");
    }
  );
  $(".headBar li.dropdown").hover(
    function () {
      $(this).find(".dropdown-menu").addClass("show");
    },
    function () {
      $(this).find(".dropdown-menu").removeClass("show");
    }
  );

  $(".closeAll").click(function () {
    $("#companyHeadTree").children("a").eq(0).siblings().remove();
    $("#mainIframe")
      .children(".iframe")
      .eq(0)
      .removeClass("hidden")
      .siblings()
      .remove();
    // $("#menuBox").find('.active').removeClass('active');
  });
}
function bindMenuEvent() {
  $("#fullSideBar a")
    .off("click")
    .on("click", function () {
      var path = $(this).data("path");
      var haschild = $(this).hasClass("hasChild");
      if (hasChild) {
        $(this).siblings(".childbox").slideDown();
      } else {
        $(this).parent("li").addClass("active");
      }
      if (path) {
        pageJump(path);
      }
    });
}
